package com.jwds.digital.bios.business.service.service.belt.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;

import com.jwds.digital.bios.business.core.infrastructure.dto.belt.assembler.BeltHealthConfigAssembler;
import com.jwds.digital.bios.business.core.infrastructure.dto.belt.BeltHealthConfigReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltExceptionDict;
import com.jwds.digital.bios.business.core.infrastructure.entity.belt.BeltHealthConfig;
import com.jwds.digital.bios.business.core.infrastructure.mapper.belt.BeltHealthConfigMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.belt.BeltHealthConfigRespVO;
import com.jwds.digital.bios.business.service.service.belt.IBeltHealthConfigService;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 皮带健康配置表实现类
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class BeltHealthConfigServiceImpl extends ServiceImpl<BeltHealthConfigMapper, BeltHealthConfig> implements IBeltHealthConfigService {

    @Autowired
    private BeltHealthConfigMapper baseMapper;


    @Override
    public List<BeltHealthConfigRespVO> findAll() {
        return baseMapper.queryList();
    }

    @Override
    public void add(BeltHealthConfigReqDTO reqDto) {
        baseMapper.insert(BeltHealthConfigAssembler.dto2entity(reqDto));
    }


    @Override
    public void update(BeltHealthConfigReqDTO reqDto) {
        BeltHealthConfig oldEntity = baseMapper.selectById(reqDto.getId());
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        BeltHealthConfig newEntity = BeltHealthConfigAssembler.dto2entity(reqDto, oldEntity);
        baseMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        BeltHealthConfig oldEntity = baseMapper.selectById(id);
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        baseMapper.deleteById(id);
    }

    @Override
    public List<BeltExceptionDict> findDictAll() {
        return baseMapper.findDictAll();
    }

    @Override
    public PageResponse<BeltHealthConfigRespVO> page(PageRequest<BeltHealthConfigReqDTO> pageReq) {
        BeltHealthConfigReqDTO queryCondition = pageReq.getQueryCondition();
        Page<BeltHealthConfigRespVO> page = new Page<>(pageReq.getPageIndex(), pageReq.getPageSize());
        IPage<BeltHealthConfigRespVO> pageData = baseMapper.pageQuery(page, queryCondition);
        return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
    }

    @Override
    public void enable(Long id) {
        BeltHealthConfig oldEntity = baseMapper.selectById(id);
        if (oldEntity==null) return;
        oldEntity.setDisabled(false);
        baseMapper.updateById(oldEntity);
    }

    @Override
    public void disable(Long id) {
        BeltHealthConfig oldEntity = baseMapper.selectById(id);
        if (oldEntity==null) return;
        oldEntity.setDisabled(true);
        baseMapper.updateById(oldEntity);

    }


}